package com.lanou.utp.controller;

import com.lanou.utp.entity.Paging;
import com.lanou.utp.entity.ResponseData;
import com.lanou.utp.entity.WarningPlan;
import com.lanou.utp.service.WarningPlanService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class WarningPlanController {

    @Resource
    private WarningPlanService warningPlanService;

    // 获取预警信息
    @GetMapping("/warning-plans")
    public ResponseData listWarningPlan(Integer type, Integer page, @RequestParam(value = "size", defaultValue = "10") Integer size) {
        List<WarningPlan> warningPlans = warningPlanService.listWarningPlans(type, page, size);
        Paging paging = warningPlanService.getPaging(type, page);
        return new ResponseData(warningPlans, paging);
    }

    // 添加预警信息
    @PostMapping("/warning-plan")
    public ResponseData addWarningPlan(@RequestBody WarningPlan warningPlan) {

        if (warningPlanService.addWarningPlan(warningPlan) == 1) {
            return new ResponseData();
        } else {
            // TODO: 2019/12/24 异常处理
            return new ResponseData(500, "后台出现异常");
        }

    }

    // 修改预警信息
    @PutMapping("/warning-plan/{id}")
    public ResponseData updateWarningPlan(@RequestBody WarningPlan warningPlan, @PathVariable("id") Integer id) {
        if (warningPlanService.updateWarningPlan(warningPlan) == 1) {
            return new ResponseData();
        } else {
            // TODO: 2019/12/24 异常处理
            return new ResponseData(500, "后台出现异常");
        }
    }

    // 删除预警信息
    @DeleteMapping("/warning-plan/{id}")
    public ResponseData deleteWarningPlan(@PathVariable("id") Integer id) {
        if (warningPlanService.deleteWarningPlan(id) == 1) {
            return new ResponseData();
        } else {
            // TODO: 2019/12/24 异常处理
            return new ResponseData(500, "后台出现异常");
        }
    }




}
